package junitTests;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

import accounts.CreateAccountServlet;

public class CreateAccountTest {
	
	private final LocalServiceTestHelper helper = 
			new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
	
	@Before
	public void prepare() {
		helper.setUp();
	}
	/*
	 * Test that when no entities are added, the datastore contains 0 entities but
	 * after an entity is created and added, the datastore contains 1 entity
	 */
	@Test
	public void AddNewUser() {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Key accountKey = KeyFactory.createKey("Accounts", "accountName");
		Query query = new Query("Account", accountKey);
        assertEquals(0, ds.prepare(query).countEntities(FetchOptions.Builder.withLimit(5)));
        Entity account = CreateAccountServlet.createEntity(accountKey, "user", "passwd", "a", "b","User");
        ds.put(account);
        assertEquals(1, ds.prepare(query).countEntities(FetchOptions.Builder.withLimit(5)));
	}
	/*
	 * Test that the information provided by the user is inserted into the entity
	 */
	@Test
	public void UserInformationStoredInAccount() {
		Key accountKey = KeyFactory.createKey("Accounts", "accountName");
		String givenUsername = "user";
		String givenPassword = "psword";
		String givenFname = "a";
		String givenLname = "b";
		String givenRole = "User";
		Entity account = CreateAccountServlet.createEntity(accountKey, givenUsername, givenPassword, givenFname, givenLname,givenRole);
		assertEquals((String)givenUsername, account.getProperty("username").toString());
		assertEquals((String)givenPassword, account.getProperty("password").toString());
		assertEquals((String)givenFname, account.getProperty("firstName").toString());
		assertEquals((String)givenLname, account.getProperty("lastName").toString());
	}
	/*
	 * Add more tests below...
	 */
	/*
	@Test
	public void tests() {
		fail();
	}
	*/

}
